package com.rmdjzz.slmmn.controller.admin.userManagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.base.User;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.pojo.vo.SongVo;
import com.rmdjzz.slmmn.pojo.vo.UserIdsVo;
import com.rmdjzz.slmmn.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者 张绍鑫
 * @创建时间 2023/6/1
 */
@Api(tags = "用户管理api文档")
@RequestMapping("/admin")
@Controller
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/userIns")
    @ResponseBody
    @ApiOperation("添加用户")
    @Deprecated
    public Result userIns(User user){
        return Result.ok(userService.save(user));
    }

    @ApiOperation(value = "用户分页")
    @GetMapping("/userPage")
    @ResponseBody
    public Result userPage(PageVo pageVo){
        //获取用户信息，不用转vo
        log.info("pageVo:" + pageVo);
        Page objectPage = new Page<>(pageVo.getStartIndex(),pageVo.getSize());
        Page userPage;
        if (pageVo.getStr()!=null && !pageVo.getStr().equals("")){
            log.info("存在模糊查询");
            QueryWrapper<User> qw=new QueryWrapper<>();
            qw.like("user_name",pageVo.getStr());
            userPage = userService.page(objectPage,qw);
        }else {
            log.info("不存在模糊查询");
            userPage = userService.page(objectPage);
        }
        log.info("songPage:"+userPage);
        return Result.ok(pageVo.getStr(),userPage);
    }


    @ApiOperation("删除用户")
    @DeleteMapping("/userDel/{id}")
    @ResponseBody
    public Result userDel(@PathVariable Integer id){
        return Result.ok(userService.removeById(id));
    }


    @ApiOperation("批量删除用户")
    @DeleteMapping("/userDelIds")
    @ResponseBody
    public Result userDelIds(UserIdsVo userIdsVo){
        return Result.ok(userService.removeBatchByIds(userIdsVo.getIds()));
    }


    @ApiOperation("根据id查询用户")
    @GetMapping("userQueryById/{id}")
    @ResponseBody
    public Result userQueryById(@PathVariable Integer id){
        return Result.ok(userService.getById(id));
    }


    @ApiOperation("更新用户")
    @PostMapping("/userUpdate/{id}")
    @ResponseBody
    public Result userUpdate(@PathVariable Integer id,User user){
        return Result.ok(userService.updateUserById(id,user));
    }

}
